<?php 

class FakeUser{

	function fakeSubmittsSimulieren($matchID, $teamWonID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		$steamID = $_SESSION['user']["steamID"];
		$ret['debug'] .= "Start fakeSubmittsSimulieren \n<br>";
		if($steamID > 0){
			if($matchID > 0){
				$TestUser = new TestUser();
				$Match = new Match();

				$sql="SELECT *
						FROM `MatchDetails`
						WHERE MatchID = ".(int) $matchID." AND Submitted != 1
									
								LIMIT 9";
				$ret['debug'] .= $sql."<br>";
				$data = $DB->multiSelect($sql);
				if(is_array($data) && count($data) > 0){
					$i=1;
					foreach($data as $k => $v){
						if($teamWonID == $v['TeamID']){
							$result = 1;
						}
						else{
							$result = -1;
						}

						$sql = "UPDATE `MatchDetails`
								SET Submitted = 1, SubmissionFor = ".$result.", SubmissionTimestamp = ".time()."
										WHERE MatchID = ".(int) $matchID." AND SteamID = ".secureNumber($v['SteamID'])."";
						$DB->update($sql);
						$ret['debug'] .= $sql."<br>";
							
						$i++;
					}
				}

				$ret['status'] = true;
			}
			else{
				$ret['status'] = "matchID 0!";
			}
		}
		else{
			$ret['status'] = "steamID 0!";
		}
		$ret['debug'] .= "End fakeSubmittsSimulieren \n<br>";
		return $ret;
	}


	function resetSubmissions($matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		$steamID = $_SESSION['user']["steamID"];
		$ret['debug'] .= "Start resetSubmissions \n<br>";
		if($steamID > 0){
			if($matchID > 0){
				$TestUser = new TestUser();
				$Match = new Match();

				$sql="SELECT *
						FROM `MatchDetails`
						WHERE MatchID = ".(int) $matchID." AND Submitted = 1 AND SteamID != ".secureNumber($steamID)."
									
								LIMIT 9";
				$ret['debug'] .= $sql."<br>";
				$data = $DB->multiSelect($sql);
				if(is_array($data) && count($data) > 0){
					$i=1;
					foreach($data as $k => $v){
						$sql = "UPDATE `MatchDetails`
								SET Submitted = 0, SubmissionFor = 0, SubmissionTimestamp = 0
								WHERE MatchID = ".(int) $matchID." AND SteamID = ".secureNumber($v['SteamID'])."";
						$DB->update($sql);
						$ret['debug'] .= $sql."<br>";
						$i++;
					}
				}

				$ret['status'] = true;
			}
			else{
				$ret['status'] = "matchID 0!";
			}
		}
		else{
			$ret['status'] = "steamID 0!";
		}
		$ret['debug'] .= "End resetSubmissions \n<br>";
		return $ret;
	}

	function insertUser($steamID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		$ret['debug'] .= "Start insertUser \n<br>";
		if($steamID > 0){
			$User = new User();
			$Login = new Login();
			if(!$User->userAlreadyInDB($steamID)){
				// neuen User eintragen und hash cookie setzen
				$User->insertNewUser($steamID);
				
				$Login->insertFirstStatsOfUser($steamID);
				$ret['status'] = "User eingetragen!";
			}
			else{
				$ret['status'] = "User schon vorhanden";
			}
		}
		else{
			$ret['status'] = "steamID 0!";
		}
		$ret['debug'] .= "End insertUser \n<br>";
		return $ret;
	}

	function insertRandomUserInQueue(){

		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		$ret['debug'] .= "Start insertRandomUserinQueue \n<br>";

		$SingleQueue = new SingleQueue();
		$Queue = new Queue();
		$matchModes = array("2","1", "3", "4", "5");
		$regions = array("1","2","3");

		$sql = "SELECT SteamID
				FROM User";
		$data = $DB->multiSelect($sql);

		if(is_array($data) && count($data) > 0){
			foreach($data as $k => $v){
				$dataNew[] = $v['SteamID'];
			}
			//p($dataNew);
			$steamID = $dataNew[array_rand($dataNew, 1)];
			//p($steamID);
			if(!$Queue->inQueue($steamID)){
				$ret = $SingleQueue->joinQueue2($matchModes, $regions, $steamID);
			}
			else{
				$ret['status'] = false;
			}
		}
		else{
			$ret['debug'] .= "SQL wrong!";
		}

		$ret['debug'] .= "End insertRandomUserinQueue \n<br>";
		return $ret;
	}

	function submitAllMatchAccept($matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		$ret['debug'] .= "Start submitAllMatchAccept \n<br>";

		if($matchID > 0){
			$sql = "UPDATE `MatchTeams`
					SET Ready = 1
					WHERE MatchID = ".(int) $matchID."
					";
			$DB->update($sql);
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "matchID 0!";
		}

		$ret['debug'] .= "End submitAllMatchAccept \n<br>";
		return $ret;
	}

}

?>